package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(int page,int pageSize){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,Orders::getUserId,userId);
        lqw.orderByDesc(Orders::getOrderTime);
        Page<Orders> page1 = ordersService.page(ordersPage, lqw);
        return R.success(page1);
    }
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, Date beginTime,Date endTime){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(number!=null,Orders::getNumber,number);
        lqw.gt(beginTime!=null,Orders::getOrderTime,beginTime)
                .lt(endTime!=null,Orders::getOrderTime,endTime);
        lqw.orderByDesc(Orders::getOrderTime);
        Page<Orders> ordersPage1 = ordersService.page(ordersPage, lqw);
        return R.success(ordersPage1);
    }
}
